package services;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: shameera
 * Date: 1/24/12
 * Time: 9:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class TempTest {
    Gson gson = new Gson();
    File file = new File("/home/shameera/Desktop/test.txt");

    // write json out put to a file
    public void writeArrayOfElementsToJson(JsonWriter writer) {
        Student student = new Student("shameera", 23);
        Student student2 = new Student("Chanaka", 24);
        String data = "test data";
        //            writer.beginArray();

        String jsonOutString = gson.toJson(student, Student.class);

        writeToFile(null);

    }
    
    public void writeToFile(String data) {

        BufferedWriter fileWriter ;//= new BufferedWriter(new FileWriter(file));
        try {
            fileWriter = new BufferedWriter(new FileWriter(file));
            fileWriter.write(data);
            fileWriter.flush();
            fileWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }

    public void writeUsingJsonWriter() {
        try {
            Writer wri = new FileWriter(file);
            JsonWriter writer = new JsonWriter(wri);
            writer.beginArray();
            writer.nullValue();
            writer.endArray();
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


    public static void main(String[] args) {
        TempTest test = new TempTest();
//        test.writeArrayOfElementsToJson(null);
    test.writeUsingJsonWriter();
    }

}
